//
//  FTHomePageViewController.m
//  项目2
//
//  Created by rimi on 16/8/19.
//  Copyright © 2016年 wzc. All rights reserved.
//

#import "FTHomePageViewController.h"
#import "FTConfigDefine.h"
#import "FTUIButton.h"
#import "FTLocationViewController.h"
#import "UIButton+FTButton.h"
#import "FTSearchButton.h"
#import "FTSearchViewController.h"
#import "UINavigationBar+FTNavi.h"
#import "UINavigationItem+FTNaviItem.h"
#import "FTHomeTableViewCell.h"
#import "FTHomePageHeaderView.h"


@interface FTHomePageViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UISearchBar *searchBar;
@property (strong, nonatomic) FTSearchButton *searchBtn;
@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end

@implementation FTHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addBackButton];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setWhiteTintColor];
    [self addSearchBtn];
    [self addNavigationItems];
    [self.view addSubview:self.tableview];
    [self.tableview registerClass:[FTHomeTableViewCell class] forCellReuseIdentifier:@"FTHomeTableViewCell"];
    self.tableview.tableHeaderView = [FTHomePageHeaderView loadHeaderView];
}
-(void)addNavigationItems{
//    self.navigationItem.titleView = self.searchBar;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"down"] style:UIBarButtonItemStyleDone target:self action:@selector(pushToLocationViewController)];
    leftButton.title = @"成都";
    FTUIButton *btn = [[FTUIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
    [btn setTitle:@"成都" forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
    btn.tintColor = [UIColor whiteColor];
    [btn addTarget:self action:@selector(pushToLocationViewController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = bbi;
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [rightBtn setImage:[UIImage imageNamed:@"二维码"] forState:UIControlStateNormal];
    [rightBtn setTintColor:[UIColor whiteColor]];
    UIBarButtonItem *bbt = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = bbt;
}
-(void)pushToQRViewController{
    PLLOG(@"456");
}
-(void)addSearchBtn{
//    self.navigationItem.titleView = self.searchBtn;
  
//    self.navigationItem.titleView = self.searchBtn;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-100 , 24)];
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = 10;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 100, 24)];
    label.text = @"项目，用户";
    label.textColor = [UIColor grayColor];
    [view addSubview:self.searchBtn];
    [view addSubview:label];
    self.navigationItem.titleView = view;
    [self.searchBtn addTarget:self action:@selector(pushToSearchViewController) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pushToSearchViewController{
    FTSearchViewController *vc = [[FTSearchViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(UIButton *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [[FTSearchButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-100, 24)];
        _searchBar.backgroundColor = [UIColor whiteColor];
        [_searchBtn setImage:[UIImage imageNamed:@"放大镜"] forState:UIControlStateNormal];
        [_searchBtn setTitle:@"项目，用户" forState:UIControlStateNormal];
        [_searchBar setTintColor:[UIColor grayColor]];
        
    }
    return _searchBtn;
}
-(void)pushToLocationViewController{
    PLLOG(@"123");
    FTLocationViewController *vc = [[FTLocationViewController alloc]init];

    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, kScreenWidth/2, kScreenWidth/3, 44)];
        _searchBar.center = CGPointMake(kScreenWidth/2, 30);
        _searchBar.placeholder = @"项目，用户";
        _searchBar.delegate = self;
    }
    return _searchBar;
}
#pragma mark --Delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else{
        return self.dataSource.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FTHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FTHomeTableViewCell" forIndexPath:indexPath];
    return cell;
}
#pragma mark --Lazy
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]initWithObjects:@"123",@"321", nil];
    }
    return _dataSource;
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [UIColor blueColor];
    }
    return _tableview;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
